//
//  CashierVC.m
//  DiningCar-Business
//
//  Created by 吕晶 on 2020/4/7.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "CashierVC.h"
#import "CashierNavBar.h"
#import "GCBSearchBar.h"
#import "CashierClassModel.h"
#import "ClassLeftCell.h"
#import "ClassRightCell.h"
#import "CashierBuyCarView.h"

@interface CashierVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) CashierNavBar *navBar;
@property (nonatomic, strong) NSArray <CashierClassModel *>*dataArray;
@property (nonatomic, strong) UITableView *leftTableView;
@property (nonatomic, strong) UITableView *rightTableView;
@property (nonatomic, strong) CashierBuyCarView *buyCarView;

@end

@implementation CashierVC {
    BOOL _rightTVScrollUp;
    BOOL _rightTVScrollDown;
    CGFloat _oldRightOffsetY;
    BOOL _didSelectLeftTVCell;//选中左边tableView cell
}

-(CashierBuyCarView *)buyCarView {
    if (!_buyCarView) {
        _buyCarView = [[CashierBuyCarView alloc] initWithFrame:CGRectMake(0, self.leftTableView.nim_bottom, ScreenW, 50)];
    }
    return _buyCarView;
}

-(UITableView *)leftTableView {
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, 90, ScreenH - self.navBar.nim_bottom - 50 - TabbarHeight)];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.backgroundColor = HexColor(0xffffff);
    }
    return _leftTableView;
}

-(UITableView *)rightTableView {
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.leftTableView.nim_right, self.navBar.nim_bottom, ScreenW - self.leftTableView.nim_width, self.leftTableView.nim_height)];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTableView.backgroundColor = HexColor(0xF0F2F5);
    }
    return _rightTableView;
}

-(CashierNavBar *)navBar {
    if (!_navBar) {
        _navBar = [[CashierNavBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NaviHeight + GCBSearchBarHeight)];
    }
    return _navBar;
}

-(NSArray<CashierClassModel *> *)dataArray {
    if (!_dataArray) {
        CashierClassModel *class1 = [CashierClassModel new];
        class1.class_name = @"分类1";
        CashierClassGoodModel *good = [CashierClassGoodModel new];
        good.title = @"商品名字";
        good.last_amount = @"100";
        good.price = @"10";
        class1.list = @[good,good,good,good,good,good,good,good];
        
        CashierClassModel *class2 = [CashierClassModel new];
        class2.class_name = @"分类2";
        class2.list = @[good,good,good,good,good,good,good,good];
        
        CashierClassModel *class3 = [CashierClassModel new];
        class3.class_name = @"分类2";
        class3.list = @[good,good,good,good,good,good,good,good];
        
        _dataArray = [NSArray array];
        _dataArray = @[class1,class2,class3];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(0xF0F2F5);
    self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.leftTableView];
    [self.view addSubview:self.rightTableView];
    [self.view addSubview:self.buyCarView];
    self.buyCarView.goodArray = @[];
    if (self.dataArray.count>0) {
        NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [_leftTableView selectRowAtIndexPath:targetIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.leftTableView) {
        return 1;
    }else {
        return self.dataArray.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView==self.leftTableView) {
        return self.dataArray.count;
    }else {
        return self.dataArray[section].list.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        ClassLeftCell *cell = [ClassLeftCell ClassLeftCellWithTableView:tableView];
        cell.classModel = self.dataArray[indexPath.section];
        return cell;
    }else {
        ClassRightCell *cell = [ClassRightCell ClassRightCellWithTableView:tableView];
        cell.goodModel = self.dataArray[indexPath.section].list[indexPath.row];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.leftTableView) {
        ClassLeftCell *cell = [ClassLeftCell ClassLeftCellWithTableView:tableView];
        cell.classModel = self.dataArray[indexPath.section];
        return cell.height;
    }else {
        ClassRightCell *cell = [ClassRightCell ClassRightCellWithTableView:tableView];
        cell.goodModel = self.dataArray[indexPath.section].list[indexPath.row];
        return cell.height;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW - 90, 35)];
    if (tableView==self.rightTableView) {
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenW - 90 - 20, 15)];
        [view addSubview:lable];
        lable.font = [UIFont systemFontOfSize:14];
        lable.textColor = HexColor(0x2B2C44);
        lable.text = self.dataArray[section].class_name;
    }
    view.backgroundColor = HexColor(0xF0F2F5);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    CGFloat headerHeight = tableView == self.rightTableView ? 35 : 0.01;
    return headerHeight;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView==self.rightTableView&&!_didSelectLeftTVCell) {
        if (scrollView.contentOffset.y <= 0) {//rightTableView不能小于最小值（不能下滑的条件）
            scrollView.contentOffset =CGPointZero;
        }
        //联动逻辑：rightTableViews顶部section头消失出现 实现 leftTableView选择联动
        if (scrollView.contentOffset.y>_oldRightOffsetY) {
            _rightTVScrollUp = YES;
            _rightTVScrollDown =  !_rightTVScrollUp;
        } else if (scrollView.contentOffset.y<_oldRightOffsetY) {
            _rightTVScrollUp = NO;
            _rightTVScrollDown =  !_rightTVScrollUp;
        }
        _oldRightOffsetY = floorf(scrollView.contentOffset.y);
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (scrollView==self.rightTableView) {
        _didSelectLeftTVCell = NO;
        _oldRightOffsetY = floorf(scrollView.contentOffset.y);
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView==self.leftTableView) {
        [self scrollRightTableViewWhenSelectRowInLeftTableViewAtIndexPath:indexPath];
        _didSelectLeftTVCell = YES;
    }
}

-(void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section {
    if (!_didSelectLeftTVCell&&tableView==self.rightTableView&&_rightTVScrollUp) {
        NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:section+1 inSection:0];
        if ((section+1)<self.dataArray.count) {
            [_leftTableView selectRowAtIndexPath:targetIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        }
    }
}
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(nonnull UIView *)view forSection:(NSInteger)section {
    if (!_didSelectLeftTVCell&&tableView==self.rightTableView&&_rightTVScrollDown) {
        NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:section inSection:0];
        [_leftTableView selectRowAtIndexPath:targetIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
    }
}

-(void)scrollRightTableViewWhenSelectRowInLeftTableViewAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.row;
    NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:NSNotFound inSection:section];
    [_rightTableView scrollToRowAtIndexPath:targetIndexPath atScrollPosition: UITableViewScrollPositionTop animated:YES];
}

@end
